package com.hzh.cusviewdemo.Util;

import android.util.Log;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 网络操作工具类
 *
 * @Desc 采用Retrofit
 */
public class RetrofitUtils {
    private static RetrofitUtils retrofitUtils;

    private RetrofitUtils() {
    }

    public static RetrofitUtils getInstance() {
        if (retrofitUtils == null) {
            synchronized (RetrofitUtils.class) {
                if (retrofitUtils == null) {
                    retrofitUtils = new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }

    public OkHttpClient.Builder getBuilder() {
        return new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)  // 60
                .writeTimeout(60, TimeUnit.SECONDS)  // 60
                .addInterceptor(httpLoggingInterceptor);
    }

    public Retrofit getRetrofit() {
        // 自定义请求头
        OkHttpClient httpClient = getBuilder()
                .addInterceptor(chain -> {
                    // 以拦截到的请求为基础创建一个新的请求对象，然后插入Header
                    // 此处没有设置token请求头，在需要配置的时候设置即可
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("connection", "keep-alive");
                    Request request = builder.build();
                    // 添加这个请求头进行请求
                    return chain.proceed(request);
                }).build();
        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(Config.MUSIC_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public Retrofit getMusicListRetrofit() {
        // 自定义请求头
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(chain -> {
                    // 以拦截到的请求为基础创建一个新的请求对象，然后插入Header
                    // 此处没有设置token请求头，在需要配置的时候设置即可
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("connection", "keep-alive");
                    Request request = builder.build();
                    // 添加这个请求头进行请求
                    return chain.proceed(request);
                }).build();
        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(Config.MUSIC_LIST_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获取每日推荐歌曲
     * @return
     */
    public Retrofit getTodayMusicRetrofit() {
        // 自定义请求头
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(chain -> {
                    // 以拦截到的请求为基础创建一个新的请求对象，然后插入Header
                    // 此处没有设置token请求头，在需要配置的时候设置即可
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("connection", "keep-alive");
                    Request request = builder.build();
                    // 添加这个请求头进行请求
                    return chain.proceed(request);
                }).build();
        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(Config.MUSIC_DAY_MUSIC_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获取音乐URL
     *
     * @return
     */
    public Retrofit getMusicUrlRetrofit() {
        // 自定义请求头
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(chain -> {
                    // 以拦截到的请求为基础创建一个新的请求对象，然后插入Header
                    // 此处没有设置token请求头，在需要配置的时候设置即可
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("connection", "keep-alive");
                    Request request = builder.build();
                    // 添加这个请求头进行请求
                    return chain.proceed(request);
                }).build();
        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(Config.MUSIC_GTE_MUSIC_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获取音乐URL
     *
     * @return
     */
    public Retrofit getMusicInfoRetrofit() {
        // 自定义请求头
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(chain -> {
                    // 以拦截到的请求为基础创建一个新的请求对象，然后插入Header
                    // 此处没有设置token请求头，在需要配置的时候设置即可
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("connection", "keep-alive");
                    Request request = builder.build();
                    // 添加这个请求头进行请求
                    return chain.proceed(request);
                }).build();
        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(Config.MUSIC_GTE_INFO_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获取音乐评论URL对应的Retrofit
     *
     * @return
     */
    public Retrofit getMusicCommentRetrofit() {
        // 自定义请求头
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(chain -> {
                    // 以拦截到的请求为基础创建一个新的请求对象，然后插入Header
                    // 此处没有设置token请求头，在需要配置的时候设置即可
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("connection", "keep-alive");
                    Request request = builder.build();
                    // 添加这个请求头进行请求
                    return chain.proceed(request);
                }).build();
        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(Config.MUSIC_COMMENT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获取我的服务器的URL
     *
     * @return
     */
    public Retrofit getMyRetrofit() {
        // 自定义请求头
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(chain -> {
                    // 以拦截到的请求为基础创建一个新的请求对象，然后插入Header
                    // 此处没有设置token请求头，在需要配置的时候设置即可
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("connection", "keep-alive");
                    Request request = builder.build();
                    // 添加这个请求头进行请求
                    return chain.proceed(request);
                }).build();
        return new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(Config.MY_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * Http访问的Log日志信息
     */
    private final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor((String message) -> {
        try {
            Log.d(Config.TAG, "log: " + message);
        } catch (Exception e) {
            Log.e(Config.TAG, "error log: " + message);
        }
    }).setLevel(HttpLoggingInterceptor.Level.BODY);

}
